#!/bin/bash
#
# Copyright 2015-2020, Guilherme Jardim
# Copyright 2022-2025, Dan Landon
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2,
# as published by the Free Software Foundation.
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#

DOCROOT=`grep -Po '^chdir = \K.*' /etc/php-fpm.d/www.conf 2>/dev/null`
if [ -z ${DOCROOT} ];then
	DOCROOT="/usr/local/emhttp"
fi

# Path to the devs.ini file
ini_file="${DOCROOT}/state/devs.ini"

# Path to the /tmp/.preclear directory
preclear_dir="/tmp/.preclear"

# Find directories in /tmp/.preclear/ and iterate over them
find "$preclear_dir" -mindepth 1 -maxdepth 1 -type d -exec bash -c '
    # Extract the device from the directory name
    device=$(basename "$1" | cut -d "_" -f 4)

    # Check if the device is in the ini file and if the "$1/pause" file exists
    if grep -q "device=\"$device\"" "$2" && [ -e "$1/pause" ]; then
        echo "Resuming preclear of disk '\''$device'\''" | logger -t preclear.disk
        rm -f "$1/pause"
    fi
' _ {} "$ini_file" \;
